﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClockLib
{
    public sealed class Clock
    {
        public Clock()
        {
            clockData = new ClockData();
        }
        private ClockData clockData;
        public ClockData ClockData {
            get {
                return clockData;
            }
            set {
                ClockData old = clockData;
                clockData = value;
            }
        }

        //public async bool RequestAlarmAccsess()
        //{
        //    return false;
        //}

        internal bool IsAlarted
        {
            get
            {
                return clockData.Trig_time <= DateTimeOffset.Now.ToUnixTimeSeconds();
            }
        }


        public void ResetTrigTime()
        {
            ClockData old =new ClockData(clockData);
            if (clockData.Repeat_type == RepeatType.NoRepeat)
            {
                return;
            }
            if (DateTimeOffset.Now.ToUnixTimeSeconds() < clockData.Trig_time) return;
            switch (clockData.Repeat_type)
            {
                case RepeatType.EveryDay:
                    int interval = int.Parse(clockData.Repeat_interval);
                    clockData.Trig_time += 60 * 60 * 24 * interval;
                    break;
                case RepeatType.EverySecond:
                    interval = int.Parse(clockData.Repeat_interval);
                    clockData.Trig_time += interval;
                    break;
                case RepeatType.EveryWeek:
                    int today = DateTimeOffset.Now.ToLocalTime().DayOfWeek.GetHashCode();
                    if (today == 0) today += 7;
                    int nextDay = today;
                    int DaySpan = 0;
                    do
                    {
                        DaySpan++;
                        nextDay += 1;
                        if (nextDay > 7) nextDay -= 7;
                    }
                    while (clockData.Repeat_interval[6 - nextDay + 1] == '0');
                    clockData.Trig_time += 60 * 60 * 24 * DaySpan;
                    break;
                default:
                    break;
            }
        }
    }
}
